﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YouWei.Api.Common
{
    public static class ObjectHelper
    {
        ///// <summary>
        ///// C#中的类extend方法，将source对象属性合并到target中
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="target"></param>
        ///// <param name="source"></param>
        //public static void ExtendTo<T>(this T source, T target)
        //{
        //    Type t = typeof(T);

        //    var properties = t.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);

        //    foreach (var prop in properties)
        //    {
        //        var propType = prop.PropertyType;
        //        var value = prop.GetValue(source, null);
        //        if (propType.Name == "DateTime" && (DateTime)value != DateTime.MinValue)
        //        {
        //            prop.SetValue(target, value, null);
        //        }
        //        else if (value != null)
        //        {
        //            prop.SetValue(target, value, null);
        //        }
        //    }
        //}

        //public static void MergeTo<Source,Target>(this Source source, Target target)
        //{
        //    Type tSource = typeof(Source);
        //    Type tTarget = typeof(Target);
        //    var sourceProps = tSource.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
        //    var targetProps = tTarget.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
        //    foreach (var sourceProp in sourceProps)
        //    {
        //        var value = sourceProp.GetValue(source, null);
        //        var targetProp = targetProps.SingleOrDefault(m => m.Name == sourceProp.Name);
        //        if (targetProp != null)
        //        {
        //            targetProp.SetValue(target, value, null);
        //        }

        //    }
        //}
        

    }
}
